﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebContribContrib.Formatting.Jsonp.SampleWebHost.Models;

namespace WebContribContrib.Formatting.Jsonp.SampleWebHost.Controllers
{
    public class KPIController : ApiController
    {
        private KPIAppEntities db = new KPIAppEntities();

        // GET api/KPI
        
        public IEnumerable<KPI> GetKPIs()
        {
            return db.KPIs.AsEnumerable();
        }

        // GET api/KPI/5
        public KPI GetKPI(int id)
        {
            KPI kpi = db.KPIs.Find(id);
            if (kpi == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return kpi;
        }

        // PUT api/KPI/5
        public HttpResponseMessage PutKPI(int id, KPI kpi)
        {
            if (ModelState.IsValid && id == kpi.ID)
            {
                db.Entry(kpi).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/KPI
        public HttpResponseMessage PostKPI(KPI kpi)
        {
            if (ModelState.IsValid)
            {
                db.KPIs.Add(kpi);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, kpi);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = kpi.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/KPI/5
        public HttpResponseMessage DeleteKPI(int id)
        {
            KPI kpi = db.KPIs.Find(id);
            if (kpi == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.KPIs.Remove(kpi);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, kpi);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}